package com.kvn.test.util;

import java.util.HashMap;
import java.util.Map;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import com.kvn.test.core.TestContext;

/**
 * SpEL工具类
 * 
 * @author wzy
 * @date 2017年10月21日 下午4:55:44
 */
public class SpELUtil {
	// SpEL parser. Instances are reusable and thread-safe.
	private static ExpressionParser parser = new SpelExpressionParser();
	// SpEL的root对象设置为 TestContext
	private static EvaluationContext context = new StandardEvaluationContext(TestContext.getInstance());
	
	public static Object getValue(String key){
		return parser.parseExpression(key).getValue(context);
	}
	
	
	public static void main(String[] args) {
//		TestContext.getInstance().getScenarioParams().put("refundSettlementId", 123L);
//		String exp = "{\"refundSettlementId\":#{#map['refundSettlementId']}, \"freezingAmount\":0, \"thawAmount\":0, \"refId\":\"my001\", \"parentOrderNo\":123, \"childOrderNo\":#{null}}";
//		System.out.println(rlt);
		TestContext.getInstance().get_S().put("refundSettlementId", 123L);
		Object value = getValue("_S[refundSettlementId]");
		System.out.println(value);
		
		Map<String, Object> dRlt = new HashMap<>();
		dRlt.put("code", 200);
		dRlt.put("msg", "成功");
		TestContext.getInstance().set_D(dRlt);
		Object value2 = parser.parseExpression("_D['msg']").getValue(context);
		System.out.println(value2);
		
	}

}
